package com.cdivtc.management.service;

import com.cdivtc.management.dao.*;
import com.cdivtc.management.pojo.*;
import com.cdivtc.management.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

@Service
public class AchievementServer {
//    成果管理

    @Autowired
    private ResultTypeMapper typeMapper;
    @Autowired
    private AchPatentMapper achPatentMapper;
    @Autowired
    private AchThesisMapper achThesisMapper;
    @Autowired
    private AchProjectMapper achProjectMapper;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private ClazzMapper clazzMapper;
    @Autowired
    private FilesurlMapper filesurlMapper;
    @Autowired
    private UploadService uploadService;

    //    查询所有成果类型
    public List<ResultType> queryAllType() {
        ResultType resultType = new ResultType();
        resultType.setParenttype("1");
        List<ResultType> resulTypes = typeMapper.select(resultType);
        return resulTypes;

    }
    //    查询所有成果类型等级

    public List<ResultType> queryAllgrades() {
        ResultType resultType = new ResultType();
        resultType.setParenttype("5");
        List<ResultType> resulTypes = typeMapper.select(resultType);
        return resulTypes;

    }
    //    根据类型查找成果类型

    public List<ResultType> queryByIstype(String istype) {
        Example example = new Example(ResultType.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("parenttype", istype);
        ResultType resultType = new ResultType();
        resultType.setParenttype(istype);
        List<ResultType> resulTypes = typeMapper.selectByExample(example);
        System.out.println("resulTypes = " + resulTypes);
        return resulTypes;

    }

    //    根据成果类型id查询成果类型名字
    public ResultType queryByAchTypeId(String id) {
        System.out.println("id = " + id);
        ResultType resultType = typeMapper.selectByPrimaryKey(id);
        System.out.println("resultType = " + resultType);
        return resultType;
    }

    /**
     * @param achThesis 状态    0 未审核   1审核通过   2 退回  3下级审核不了提交上级
     */
    //添加成果
    public void addAchThesis(AchThesis achThesis) {
        String clazzId = achThesis.getClazzId();
        achThesis.setCreateDate(new Date());
        achThesis.setState(clazzId != null ? ("".equals(clazzId) ? "1" : "0") : "1");
        achThesis.setTypename(queryByAchTypeId(achThesis.getType()).getResultType());
        achThesisMapper.insert(achThesis);
    }

//    添加项目

    public void addAchProject(AchProject achProject) {
        String clazzId = achProject.getClazzId();
        achProject.setCreateDate(new Date());
        achProject.setState(clazzId != null ? ("".equals(clazzId) ? "1" : "0") : "1");
        achProject.setTypename(queryByAchTypeId(achProject.getType()).getResultType());
        achProjectMapper.insert(achProject);
    }

    //    添加专利
    public void addAchPatent(AchPatent achPatent) {

        achPatent.setCreateDate(new Date());
        achPatent.setState("1");
        achPatent.setTypename(queryByAchTypeId(achPatent.getType()).getResultType());
        achPatentMapper.insert(achPatent);
    }

    //根据用户id查询成果论文
    public List<AchThesis> queryAchThesisByUid(String uid) {

        Example example = new Example(AchThesis.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("uid", uid);
        return achThesisMapper.selectByExample(example);

    }

    public List<AchProject> queryAchProjectByUid(String uid) {

        Example example = new Example(AchProject.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("uid", uid);
        return achProjectMapper.selectByExample(example);

    }

    public List<AchPatent> queryAchPatentByUid(String uid) {
        Example example = new Example(AchPatent.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("uid", uid);
        return achPatentMapper.selectByExample(example);
    }


    //    根据用户id查询所有
    public Map queryByAchAllAndUid(String uid) {
//更护用户id查询三张表-
        List<AchThesis> achThesis = queryAchThesisByUid(uid);
        List<AchProject> achProjects = queryAchProjectByUid(uid);
        List<AchPatent> achPatents = queryAchPatentByUid(uid);
        Map map = new HashMap();
        map.put("achThesis", achThesis);
        map.put("achProjects", achProjects);
        map.put("achPatents", achPatents);
        return map;
    }

    //修改achThesis
    public void updateAchThesis(AchThesis achThesis) {
        achThesisMapper.updateByPrimaryKeySelective(achThesis);
    }

    //修改achProject
    public void updateAchPaject(AchProject achProject) {
        achProjectMapper.updateByPrimaryKeySelective(achProject);
    }

    //修改achpatent
    public void updateAchPatent(AchPatent achPatent) {
        achPatentMapper.updateByPrimaryKeySelective(achPatent);
    }
    //    根据成果id查询成果信息

    public Map queryAchById(String id) {
        AchThesis achThesis = achThesisMapper.selectByPrimaryKey(id);
        AchProject achProject = achProjectMapper.selectByPrimaryKey(id);
        AchPatent achPatent = achPatentMapper.selectByPrimaryKey(id);
        Map map = new HashMap();
        List<Filesurl> filesurlList = new ArrayList<>();
        if (achThesis != null) {
            map.put("ach", achThesis);
            filesurlList = deletefile(achThesis.getUid(), achThesis.getId());
        }
        if (achProject != null) {
            map.put("ach", achProject);
            filesurlList = deletefile(achProject.getUid(), achProject.getId());
        }
        if (achPatent != null) {
            map.put("ach", achPatent);
            filesurlList = deletefile(achPatent.getUid(), achPatent.getId());
        }
        map.put("files", filesurlList);
        return map;
    }


    public List queryAchByCollage(String collage) {


        List<AchThesis> achThesisList = achThesisMapper.queryAchByCollage(collage);
        List<AchProject> achProjectList = achProjectMapper.queryAchByCollage(collage);
        List<AchPatent> achPatentList = achPatentMapper.queryAchByCollage(collage);

        List list = new ArrayList();
        list.addAll(achThesisList);
        list.addAll(achProjectList);
        list.addAll(achPatentList);

        return list;
    }

    public List queryAchByclassAndTeacher(String teacherid) {
//        查询出辅导员所管理的班级
//        老师不能审核

        Clazz clazz = new Clazz();
        clazz.setCounsellor(teacherid);
        List<Clazz> clazzList = clazzMapper.select(clazz);
        List<AchThesis> achThesisList = new ArrayList<>();
        List<AchProject> achProjectList = new ArrayList<>();
        for (Clazz clazz1 : clazzList) {
            List<AchThesis> select = achThesisMapper.queryAchByClazz(clazz1.getClazzId());
            List<AchProject> select1 = achProjectMapper.queryAchByClazz(clazz1.getClazzId());
            achProjectList.addAll(select1);
            achThesisList.addAll(select);
        }
        List list = new ArrayList();
        list.addAll(achThesisList);
        list.addAll(achProjectList);


        return list;
    }
    public List queryAchByclassAndTeacherpass(String teacherid,String grades,String state) {
//        查询出辅导员所管理的班级
//        老师不能审核
        Clazz clazz = new Clazz();
        clazz.setCounsellor(teacherid);
        List<Clazz> clazzList = clazzMapper.select(clazz);
        List<AchThesis> achThesisList = new ArrayList<>();
        List<AchProject> achProjectList = new ArrayList<>();
        for (Clazz clazz1 : clazzList) {
            List<AchThesis> select = achThesisMapper.queryAchByClazzpass(clazz1.getClazzId());
            achThesisList.addAll(select);
            List<AchProject> select1 = achProjectMapper.queryAchByClazzpass(clazz1.getClazzId());
            achProjectList.addAll(select1);
        }
        List list = new ArrayList();
        list.addAll(achThesisList);
        list.addAll(achProjectList);


        return list;
    }


    public void uppdataAchstate(String achid,String state,String comment) {
        AchThesis achThesis = new AchThesis();
        achThesis.setId(achid);
        achThesis.setState(state);
        achThesis.setComment(comment);
        achThesis.setUpdateDate(new Date());
        achThesisMapper.updateByPrimaryKeySelective(achThesis);
        AchProject achProject = new AchProject();
        achProject.setId(achid);
        achProject.setState(state);
        achProject.setComment(comment);
        achProject.setUpdateDate(new Date());
        achProjectMapper.updateByPrimaryKeySelective(achProject);
        AchPatent achPatent = new AchPatent();
        achPatent.setId(achid);
        achPatent.setState(state);
        achPatent.setComment(comment);
        achPatent.setUpdateDate(new Date());
        achPatentMapper.updateByPrimaryKeySelective(achPatent);


    }


    public void uppdataAchsubmission(String achid,String state,String comment,String submission) {
        AchThesis achThesis = new AchThesis();
        achThesis.setId(achid);
        achThesis.setState(state);
        achThesis.setComment(comment);
        achThesis.setSubmission(submission);
        achThesis.setUpdateDate(new Date());
        achThesisMapper.updateByPrimaryKeySelective(achThesis);
        AchProject achProject = new AchProject();
        achProject.setId(achid);
        achProject.setState(state);
        achProject.setComment(comment);
        achProject.setSubmission(submission);
        achProject.setUpdateDate(new Date());
        achProjectMapper.updateByPrimaryKeySelective(achProject);
        AchPatent achPatent = new AchPatent();
        achPatent.setId(achid);
        achPatent.setState(state);
        achPatent.setComment(comment);
        achPatent.setSubmission(submission);
        achPatent.setUpdateDate(new Date());
        achPatentMapper.updateByPrimaryKeySelective(achPatent);


    }


    public List queryAchBySchool() {

        List<AchThesis> select = achThesisMapper.queryAchBySchool();

        List<AchProject> select1 = achProjectMapper.queryAchBySchool();

        List<AchPatent> select2 = achPatentMapper.queryAchBySchool();

        List list = new ArrayList();
        list.addAll(select);
        list.addAll(select1);
        list.addAll(select2);

        return list;
    }

    public Map queryByAchwhereuid(String title, String uid) {
        Map map = new HashMap();
        title = "%"+title+"%";
        List<AchThesis> achThesisList = achThesisMapper.queryByAchTitleAndUid(uid,title);
        List<AchProject> achProjectList = achProjectMapper.queryByAchTitleAndUid(uid,title);
        List<AchPatent> achPatentList = achPatentMapper.queryByAchTitleAndUid(uid,title);
        map.put("achThesis", achThesisList);
        map.put("achPatent", achPatentList);
        map.put("achProject", achProjectList);

        return map;
    }

    public void updateFiles(List<String> list, String id) {
        for (String s : list) {
            Filesurl filesurl = filesurlMapper.selectByPrimaryKey(s);
            filesurl.setTaskId(id);
            System.out.println("s = " + filesurl);
            filesurlMapper.updateByPrimaryKey(filesurl);
        }
    }

    public List<Filesurl> deletefile(String uid, String task) {
        Example example = new Example(Filesurl.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId", uid);
        criteria.andEqualTo("taskId", task);
        List<Filesurl> filesurls = filesurlMapper.selectByExample(example);

        return filesurls;
    }

    public List queryAchByCollagepass(String collage) {
//        院长查看审核通过的

        List<AchThesis> select = achThesisMapper.queryAchByCollagepass(collage);
        List<AchProject> select1 = achProjectMapper.queryAchByCollagepass(collage);
        List<AchPatent> select2 = achPatentMapper.queryAchByCollagepass(collage);

        List list = new ArrayList();
        list.addAll(select);
        list.addAll(select1);
        list.addAll(select2);


        return list;
    }

    public List queryAchBySchoolpass() {
        List<AchThesis> select = achThesisMapper.queryAchBySchoolpass();
        List<AchProject> select1 = achProjectMapper.queryAchBySchoolpass();
        List<AchPatent> select2 = achPatentMapper.queryAchBySchoolpass();

        List list = new ArrayList();
        list.addAll(select);
        list.addAll(select1);
        list.addAll(select2);
        return list;
    }
}
